package com.scalar.chiptrack.finance.openpo.actions;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.finance.openpo.actioncommands.PurchaseOrderActionCommandFactory;
import com.scalar.chiptrack.users.ErrorReport;
import com.scalar.chiptrack.utils.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Phani Raja Kumar
 * Date: Feb 16, 2005
 * Time: 7:10:27 PM
 * To change this template use Options | File Templates.
 */


public class PurchaseOrderAction extends ChipTrackExtendedAction
{
    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {

        HashMap errors  = new HashMap();
        HashMap info    = new HashMap();
        ActionForward actionForward         = mapping.findForward( "search" );
        String action   = request.getParameter( REQUEST_ACTION );
        ChipTrackExtendedAction purchaseOrderCommand  = null;

		purchaseOrderCommand = PurchaseOrderActionCommandFactory.getActionCommand( action );
		if( purchaseOrderCommand == null )
        {
        	Logger.error( "" );
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_CREATE_PURCHASE_ORDER_INSTANCE );
            actionForward = mapping.findForward("search") ;
        }
        else
        {
			try
            {
            	actionForward = purchaseOrderCommand.execute( mapping, form, request, response );
				errors  = (HashMap)request.getAttribute( ERROR_MESSAGES );
				info    = (HashMap)request.getAttribute( INFO_MESSAGES );
			}
            catch ( Exception e )
            {
                Logger.error( ERROR_COULD_NOT_CREATE_PURCHASE_ORDER_INSTANCE );
                errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_CREATE_PURCHASE_ORDER_INSTANCE );
                pSession.setAttribute( ERROR_REPORT_OBJECT, new ErrorReport( "Error while executing the command", e ) );
                actionForward = mapping.findForward("search") ;
			}
		}

		request.setAttribute( ERROR_MESSAGES, errors );
		request.setAttribute( INFO_MESSAGES, info );
        return actionForward;
    }
}